package com.songaw.generator.common.exception;

import com.songaw.generator.common.pojo.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.AbstractErrorController;
import org.springframework.boot.autoconfigure.web.DefaultErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 全局异常  其他异常*/


@Controller
@Slf4j
public class GlobalExceptionController extends AbstractErrorController {
    private final ErrorProperties errorProperties;

    public GlobalExceptionController(){
        this(new DefaultErrorAttributes());
    }
    public GlobalExceptionController(ErrorAttributes errorAttributes) {
        this(errorAttributes, new ErrorProperties());
    }
    public GlobalExceptionController(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        super(errorAttributes);
        Assert.notNull(errorProperties, "ErrorProperties must not be null");
        this.errorProperties = errorProperties;
    }

    private static final String PATH = "/error";

    @Override
    public String getErrorPath() {
        return PATH;
    }

    @RequestMapping(value = "${server.error.path:${error.path:/error}}",produces="application/json;charset=UTF-8")
    @ResponseBody
    public ResponseEntity error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        log.error("url:{},error:{},message:{}", body.get("path"), body.get("error"), body.get("message"));
        switch (status){
            case NOT_FOUND:
                NotFoundException ne = new NotFoundException("error.404");
                return new ResponseEntity(ne.getResult(), status);
            case BAD_REQUEST:
                return new ResponseEntity<Result>(  new Result("error.400"),status);
            case SERVICE_UNAVAILABLE:
                return new ResponseEntity<Result>(  new Result("error.500"),status);
            case UNSUPPORTED_MEDIA_TYPE:
                return new ResponseEntity<Result>(new Result("error.415","不支持次请求"),status);
            case FORBIDDEN:
                return new ResponseEntity<Result>(new Result("error.403"), HttpStatus.FORBIDDEN);
            case UNAUTHORIZED:
                return new ResponseEntity<Result>( new Result("error.401"), HttpStatus.UNAUTHORIZED);
                default:
                    return new ResponseEntity<Result>(  new Result("error.500"),status);
        }
    }

    protected ErrorProperties getErrorProperties() {
        return this.errorProperties;
    }

    protected boolean isIncludeStackTrace(HttpServletRequest request, MediaType produces) {
        ErrorProperties.IncludeStacktrace include = getErrorProperties().getIncludeStacktrace();
        if (include == ErrorProperties.IncludeStacktrace.ALWAYS) {
            return true;
        }
        if (include == ErrorProperties.IncludeStacktrace.ON_TRACE_PARAM) {
            return getTraceParameter(request);
        }
        return false;
    }


}
